package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hitqz.robot.api.business.entity.JobModeEntity;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.config.RobotProperties;
import com.hitqz.robot.biz.flow.config.FlowInterruptException;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.BusinessService;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @Author: Wang Weiwei
 * @Description: TODO2
 * @DateTime: 2024/5/6 10:44
 **/
@Slf4j
@LiteflowComponent(value = "GoMiddleWaitForStandBy", name = "[轮对/轮轴]前往中间站点-等待区域")
@AllArgsConstructor
public class GoMiddleWaitForStandBy extends NodeComponent {
    private final BusinessService businessService;

    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final DispatchService dispatchService;

    private final RobotProperties robotProperties;

    private final RestTemplate restTemplate;

    private final String url = "http://127.0.0.1:9999/admin/robot/getJobMode";


    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        String response = restTemplate.getForObject(url, String.class);
        JSONObject responseJson = JSONUtil.parseObj(response);
        String data = responseJson.getStr("data");
        JobModeEntity jobMode = JSONUtil.toBean(data, JobModeEntity.class);
        if ("lundui".equals(robotProperties.getSystemType()) && "2".equals(jobMode.getMode())) {
            log.info("当前，轮对系统，且双车模式");
            String key = FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0);
            Integer cmd = this.getCmpData(JSONObject.class).get(key, Integer.class);
            cmd = Math.max(0, cmd);
            log.info("[liteFlow]前往中间站点，等待位置:{}", cmd);
            RobotMapPos robotMapPos = robotMapPosService.getById(cmd);
            dispatchService.go2TargetPosition(robotMapPos, false);
            TimeUnit.SECONDS.sleep(3);
            IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
            while (true) {
                try {
                    iRobotPosDto = robotDriverService.getWebPos();
                    if (iRobotPosDto != null
                            && iRobotPosDto.getLastStation() != null
                            && robotMapPos.getCode().equals(iRobotPosDto.getLastStation())
//                            && iRobotPosDto.isReached()
                    ) {
                        log.info("机器人到达位置");
                        break;
                    }
                    LiteFlowUtil.checkInterruptFlag();
                    TimeUnit.SECONDS.sleep(1);
                } catch (FlowInterruptException e) {
                    break;
                }
            }
        }
    }
}